﻿function hightLight(str)
{
	var start_var = false;
	if(str.substring(0,3) == "var")
	{
		start_var = true;
	}
	var keywords_green = "= { }".split(" ");
	var keywords_blue = "function new return var typeof instanceof".split(" ");
	var keywords_red = "break case catch continue default delete else finally for if switch this throw try void while with ;".split(" ");
	str = str.replace(/};/g,"};\n");
	for(var i = 0; i < keywords_green.length; i++)
	{
		var oReg = new RegExp(keywords_green[i], "g");
		var rp = "<span style='color:green'>" + keywords_green[i] + "</span>";
		str = str.replace(oReg,rp);
	}
	var rp3 = "<span style='background-color:yellow'>$1var</span>";
	str = str.replace(/([^;|{|\t| ])var/g,rp3);
	if(start_var)
	{
		str = str.replace(/<span style='background-color:yellow'>var<\/span>/,"var");
	}
	for(var i = 0; i < keywords_blue.length; i++)
	{
		var oReg = new RegExp(keywords_blue[i], "g");
		var rp = "<span style='color:blue'>" + keywords_blue[i] + "</span>";
		str = str.replace(oReg,rp);
	}
	for(var i = 0; i < keywords_red.length; i++)
	{
		var oReg = new RegExp(keywords_red[i], "g");
		var rp = "<span style='color:red'>" + keywords_red[i] + "</span>";
		var rp2 = "$1<span style='color:red'>" + keywords_red[i] + "</span>(";
		if(keywords_red[i] == "if")
		{
			str = str.replace(/if(\s?)\(/g,rp2);
		}
		else if(keywords_red[i] == "for")
		{
			str = str.replace(/for(\s?)\(/g,rp2);
		}
		else
		{
			str = str.replace(oReg,rp);
		}
	}
	str = str.replace(/\n/g,"<br />");
	str = str.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
	return str;
 }
 function init()
 {
 	var obj = document.getElementById("showCode");
	var str = obj.innerText || obj.textContent;
	obj.innerHTML = hightLight(str);
 }
 init();
